package com.lh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lh.entity.table.DeadlineRectificationPeriod;
import com.lh.mapper.DeadlineRectificationPeriodMapper;
import com.lh.service.DeadlineRectificationPeriodService;
import com.lh.utils.Answer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DeadlineRectificationPeriodServiceImpl implements DeadlineRectificationPeriodService {
    @Resource
    private DeadlineRectificationPeriodMapper deadlineRectificationPeriodMapper;

    @Override
    public Answer<Object> addDeadlineRectificationPeriod(DeadlineRectificationPeriod deadlineRectificationPeriod) {
        deadlineRectificationPeriod.setIsApproval(0);
        DeadlineRectificationPeriod period = deadlineRectificationPeriodMapper.selectOne(new LambdaQueryWrapper<DeadlineRectificationPeriod>()
                .eq(DeadlineRectificationPeriod::getDeadlineNodeId, deadlineRectificationPeriod.getDeadlineNodeId())
                .eq(DeadlineRectificationPeriod::getIsApproval, 0));
        if (period!=null){
            return Answer.ok("已经有一个待审核数据，请先前往审核");
        }else {
            deadlineRectificationPeriodMapper.insert(deadlineRectificationPeriod);
            return Answer.ok("添加成功");
        }
    }

    @Override
    public Answer<Object> reviewDeadlineRectificationPeriod(DeadlineRectificationPeriod deadlineRectificationPeriod) {
        deadlineRectificationPeriodMapper.updateById(deadlineRectificationPeriod);
        return Answer.ok("审核成功");
    }

    @Override
    public Answer<Object> getDeadlineRectificationPeriod(DeadlineRectificationPeriod deadlineRectificationPeriod) {
        Integer id = deadlineRectificationPeriod.getId();
        if (id==null){
            return Answer.error("id不能为空");
        }
        DeadlineRectificationPeriod deadlineRectificationPeriod1 = deadlineRectificationPeriodMapper.selectOne(new LambdaQueryWrapper<DeadlineRectificationPeriod>()
                .eq(DeadlineRectificationPeriod::getId, id));
        return Answer.ok(deadlineRectificationPeriod1);
    }

    @Override
    public Answer<Object> getDeadlineRectificationPeriodListByNodeId(DeadlineRectificationPeriod deadlineRectificationPeriod) {
        Integer nodeId = deadlineRectificationPeriod.getDeadlineNodeId();
        if (nodeId==null){
            return Answer.error("id不能为空");
        }
        List<DeadlineRectificationPeriod> deadlineRectificationPeriodList = deadlineRectificationPeriodMapper.selectList(new LambdaQueryWrapper<DeadlineRectificationPeriod>()
                .eq(DeadlineRectificationPeriod::getDeadlineNodeId, nodeId));
        return Answer.ok(deadlineRectificationPeriodList);
    }

    @Override
    public Answer<Object> addDeadlineRectificationPeriodNoReview(DeadlineRectificationPeriod deadlineRectificationPeriod) {
        deadlineRectificationPeriod.setIsApproval(1);
        deadlineRectificationPeriodMapper.insert(deadlineRectificationPeriod);
        return  Answer.ok("添加成功");
    }


}
